//
//  ViewController.swift
//  JYRouter
//
//  Created by huangguojian on 2021/9/15.
//

import UIKit
import JYRouter
import StackKit
import ConstraintKit

class PresentViewController: UIViewController, JYRoutable {
    var routeCallBack: ((JYRouterModelType) -> ())?
    
    let routeStyle: JYRouteStyle = .present
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
        self.modalTransitionStyle = .crossDissolve
        self.modalPresentationStyle = .overFullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        let btn = createBtn("返回")
        btn.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(btn)
        
        let constraints = [btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
                           btn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
                           btn.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
                           btn.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -16)]
        
        self.view.addConstraints(constraints)
    }
    
    private func createBtn(_ title: String) -> UIButton {
        let btn = UIButton(type: .custom)
        btn.setTitle(title, for: .normal)
        btn.addTarget(self, action: #selector(back), for: .touchUpInside)
        btn.setTitleColor(.blue, for: .normal)
        btn.backgroundColor = .lightGray
        btn.titleLabel?.numberOfLines = 0
        return btn
    }
    
    @objc func back(_ sender: UIButton) {
        
        self.routeBack()
    }
}

class ViewController: UIViewController, JYRoutable {
    let routeStyle: JYRouteStyle = .push
    
    var routeCallBack: ((JYRouterModelType) -> ())?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white
        
//        let stack = UIStackView(arrangedSubviews: [
//            createBtn("https://www.baidu.com/a?a0=111&b=2222"),
//            createBtn("https://www.baidu.com/b?bbb=前置条件失败后，跳转到登录页"),
//            createBtn("https://www.baidu.com/present"),
//            createBtn("https://www.baidu.com/selectTabbar2"),
//            createBtn("https://www.baidu.com/relogin"),
//            createBtn("https://www.github.com/tip=不在白名单中，留意控制台输出"),
//        ])
//        stack.axis = .vertical
//        stack.spacing = 16
//        stack.distribution = .fill
//        stack.translatesAutoresizingMaskIntoConstraints = false
//
//        self.view.addSubview(stack)
//
//        let constraints = [stack.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
//                           stack.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
//                           stack.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16),
//                           stack.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -16)]
//
//        self.view.addConstraints(constraints)
        
        self.view.VStack(spacing: 16) {
            createBtn("app://main/a?a0=123")
            createBtn("app://main/a?a0=111&b=2222")
            createBtn("app://main/b?bbb=前置条件失败后，跳转到登录页")
            createBtn("app://main/present")
            createBtn("app://main/selectTabbar2")
            createBtn("app://main/relogin")
            createBtn("http://www.baidu.com")
        }
        .distribution(.equalSpacing)
        .withBackground(.green, cornerRadius: 8)
        .margin(.zero)
    }
    
    deinit {
        routeCallBack?(self.title ?? "")
    }

    @objc func btnOnTap(_ sender: UIButton) {
        
        AppRouterManager.linkUrl(sender.title(for: .normal) ?? "") {
            print($0)
        }
    }
    
    
    private func createBtn(_ title: String) -> UIButton {
        let btn = UIButton(type: .custom)
        btn.setTitle(title, for: .normal)
        btn.addTarget(self, action: #selector(btnOnTap), for: .touchUpInside)
        btn.setTitleColor(.blue, for: .normal)
        btn.backgroundColor = .lightGray
        return btn
    }
}

class Index2Controller: UIViewController, JYRoutable {
    var routeStyle: JYRouteStyle { .tabbarSelect }
    
    var routeCallBack: ((JYRouterModelType) -> ())?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        super.loadView()
        let label = UILabel()
        label.text = "Index2Controller"
        label.backgroundColor = .white
        label.textAlignment = .center
        self.view = label
    }
}

//MARK: -  TabBarController
class TabBarController: UITabBarController, JYRoutable {
    var routeStyle: JYRouteStyle { .windowRoot }
    
    var routeCallBack: ((JYRouterModelType) -> ())?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        super.loadView()
        
      let homeNv = NavigationController(rootRoutable: ViewController())
        homeNv.tabBarItem.title = "A"
        
        let index2Nv = NavigationController(rootRoutable: Index2Controller())
        index2Nv.tabBarItem.title = "B"
        
        self.setViewControllers([homeNv, index2Nv], animated: false
        )
    }
}

//MARK: -  导航容器
class NavigationController<RootController>: UINavigationController, JYRoutable where RootController: JYRoutable {
    var routeStyle: JYRouteStyle { rootViewController.routeStyle }
    
    var routeCallBack: ((JYRouterModelType) -> ())?
    
    
    private(set) var rootViewController: RootController!
    
    init(rootRoutable: RootController) {
        super.init(rootViewController: rootRoutable)
        self.rootViewController = rootRoutable
    }

    private override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: -  登录
class ReloginController: UIViewController, JYRoutable {
    
    var routeStyle: JYRouteStyle { .windowRoot }
    
    var routeCallBack: ((JYRouterModelType) -> ())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.addTarget(self, action: #selector(login), for: .touchUpInside)
        
        self.view.addSubview(btn)
        self.view.addConstraints([
            btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            btn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
        ])
        
    }
    
    @objc func login() {
        AppRouterManager.main.home.link()
    }
}
